import unittest
from time import sleep

from parameterized import parameterized

from page.c_user_page import UserPage
from tools.c_read_admin import read_json as admins
from tools.c_read_user import read_json as users
from tools.c_read_power import read_json as powers
from tools.c_read_mana import read_json as manas
from page import c_path
from page.c_admin_page import AdminPage

datas = admins()
users = users()
powers = powers()
manas = manas()


# 权限列表
# 管理员列表之添加管理员测试类
class AdminTest1(unittest.TestCase):
    adminPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.adminPage = AdminPage()

    @parameterized.expand(
        datas[0]
    )
    def test_add_admin(self, add, ex):
        # print(add, ex)
        self.adminPage.add_admin(*add)
        msg = self.adminPage.get_text(c_path.admin_text)
        try:
            self.assertEqual(ex, msg)
        except:
            self.adminPage.get_image("c_admin_test1")
            raise

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(3)
        cls.adminPage.driver.quit()


# 管理员列表之修改管理员信息且查询和删除管理员测试类
class AdminTest2(unittest.TestCase):
    adminPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.adminPage = AdminPage()

    @parameterized.expand(
        datas[1]
    )
    def test_update_admin(self, update, ex, search):
        # print(update, ex)
        self.adminPage.update_admin(*update)
        msg1 = self.adminPage.get_text(c_path.click_xg)
        try:
            self.assertEqual(ex, msg1)
        except:
            self.adminPage.get_image("c_admin_test2")
            raise
        self.adminPage.search_admin(search)
        sleep(1)
        self.adminPage.delete_admin()

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(3)
        cls.adminPage.driver.quit()


# 角色管理之添加用户类
class UserTest1(unittest.TestCase):
    uaerPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.uaerPage = AdminPage()

    @parameterized.expand(users[0])
    def test_add_user(self, add, ex):
        self.uaerPage.add_user(add)
        msg1 = self.uaerPage.get_text(c_path.user_text)
        try:
            self.assertEqual(ex, msg1)
            self.uaerPage.get_image("c_user_test1")
        except:
            self.uaerPage.get_image("c_er_test1")
            raise

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(2)
        cls.uaerPage.driver.quit()


# 角色管理之修改用户且查询并删除用户类
class UserTest2(unittest.TestCase):
    uaerPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.uaerPage = AdminPage()

    @parameterized.expand(users[1])
    def test_update_user(self, update, ex, search):
        self.uaerPage.update_user(update)  # 修改用户
        self.uaerPage.get_image("c_user_test2")
        msg2 = self.uaerPage.get_text(c_path.update_user_text)
        try:
            self.assertEqual(ex, msg2)
        except:
            self.uaerPage.get_image("c_er_test2")
            raise
        self.uaerPage.search_user(search)  # 查询删除用户

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(2)
        cls.uaerPage.driver.quit()


# 权限分配之添加权限类
class PowerTest1(unittest.TestCase):
    PowerPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.PowerPage = AdminPage()

    @parameterized.expand(powers[0])
    def test_power1(self, s, ex):
        self.PowerPage.add_power(*s)  # 添加权限
        sleep(1)
        # self.PowerPage.get_image("c_power_test1")
        msg1 = self.PowerPage.get_text(c_path.add_power_text)
        try:
            self.assertEqual(ex, msg1)
        except:
            self.PowerPage.get_image("c_power_er_test1")
            raise

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(2)
        cls.PowerPage.driver.quit()


# 权限分配之修改权限和删除权限
class PowerTest2(unittest.TestCase):
    PowerPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.PowerPage = AdminPage()

    @parameterized.expand(powers[1])
    def test_power2(self, s, ex):
        self.PowerPage.update_power(s)  # 修改权限
        self.PowerPage.get_image("c_power_test2")
        msg2 = self.PowerPage.get_text(c_path.update_power_text)
        try:
            self.assertEqual(ex, msg2)
        except:
            self.PowerPage.get_image("c_power_er_test2")
            raise
        sleep(1)
        self.PowerPage.delete_power()  # 删除权限

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(2)
        cls.PowerPage.driver.quit()


# 用户管理之用户列表添加用户类
class ManaTest1(unittest.TestCase):
    ManaPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.ManaPage = UserPage()

    @parameterized.expand(manas[0])
    def test_mana1(self, add, ex):
        self.ManaPage.add_mana_user(*add)  # 添加权限
        msg1 = self.ManaPage.get_text(c_path.mana_text)
        try:
            self.assertEqual(ex, msg1)
        except:
            self.ManaPage.get_image("c_mana_er_test1")
            raise

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(2)
        cls.ManaPage.driver.quit()


# 用户管理之用户列表修改与删除用户类
class ManaTest2(unittest.TestCase):
    ManaPage = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.ManaPage = UserPage()

    @parameterized.expand(manas[1])
    def test_mana2(self, update, ex):
        self.ManaPage.update_mana_user(*update)  # 修改
        msg2 = self.ManaPage.get_text(c_path.update_mana_text)
        try:
            self.assertEqual(ex, msg2)
        except:
            self.ManaPage.get_image("c_mana_er_test2")
            raise
        self.ManaPage.delete_mana_user()  # 删除用户
        msg3 = self.ManaPage.get_text(c_path.update_mana_text)
        try:
            self.assertEqual(ex, msg3)
        except:
            self.ManaPage.get_image("c_mana_er_test2")
            raise

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(2)
        cls.ManaPage.driver.quit()
